﻿package game {
	import game.objects.GameObject;
	import game.network.Network;
	import game.socket.*;
	import game.login.*;
	import game.libs.*;
	import game.errors.GameError;
	import game.ui.Loading;
	import game.debug.Debug;
	import flash.events.Event;
	import flash.events.ProgressEvent;
	import flash.display.MovieClip;
	
	
	public class Game extends GameObject {
		
		private var login:Login;
		private var loading:Loading;
		private var account;
		
		public function Game():void {
			/*	1.
				инициализация загрузчика
			*/
			
			loading = new Loading;
			loading.addEventListener(Event.REMOVED_FROM_STAGE, function (event:Event) { loading = null; });
			addChild(loading);
		}
		
		public function initNetwork():void {
			/*	2.
				инициализация соц сети
			*/
			
			network.addEventListener( ProgressEvent.PROGRESS, function (event:ProgressEvent) { loading.progress((event.target as Network).progress); });
		}
		
		public function init():void {
			/*	3.
				соц сеть получила свои данные
				инициализируем сокет
				инициализируем загрузчик библиотек
			*/
			
			if (!loading) return;
			
			loading.next();
			
			socket.addEventListener( SocketEvent.CONNECTED,		function (event:SocketEvent)	{	debug ('Подключились к ' + event.socket.data, Debug.SOCKET); });
			socket.addEventListener( SocketEvent.DISCONNECTED,	function (event:SocketEvent)	{	debug ('Соединение разорвано ' + event.socket.data, Debug.SOCKET); });
			socket.addEventListener( SocketEvent.ERROR,			function (event:SocketEvent)	{	error(GameError.SOCKET_ERROR, 0); });
			socket.addEventListener( SocketHandler.RECIVE,		function (event:SocketHandler)	{	debug ('Получено ' + String(event.socketPackage), Debug.SOCKET); });
			
			libs.addEventListener( LibsEvent.COMPLETE,		libsComplete);
			libs.addEventListener( LibsEvent.ERROR,			function (event:LibsEvent)		{ error(GameError.INIT_ERROR, 0); });
			libs.addEventListener( ProgressEvent.PROGRESS,	function (event:ProgressEvent)	{ loading.progress((event.target as Libs).progress); });
			libs.load();
			
			
			stage.addEventListener(Event.RESIZE, resized);
			
			new GameMouse ( stage );
		}
		
		private function libsComplete(event:LibsEvent):void {
			/*	4.
				все библиотеки загружены
				логинимся
			*/
			
			loading.next();
			
			loading.next();
			logined(null);
			return;
			/*login = new Login;
			login.addEventListener( LoginEvent.LOGINED, logined);
			login.addEventListener( ProgressEvent.PROGRESS,	function (event:ProgressEvent) { loading.progress((event.target as Login).progress); });
			login.start();*/
		}
		
		private function logined(event:LoginEvent):void {
			/*	5.
				залогинился
				запускаем игру
			*/
			
			openAccount();
		}
		
		private function openAccount():void {
			var account:Account = new Account;
			
			addChildAt(account, 0);
		}
		
		private function resized(event:Event):void {
			
			for (var i:int = 0; i<this.numChildren; i++) {
				
			}
			
		}
	}
}